cap frame change default
cap frame drop results
cap frame drop disclosure
frame create results strL(label) b_evict se_evict n_obs y_base_mean y_base_sd y_base_n sd_y_all sd_evict_all b_judgmentamount se_judgmentamount

global cluster_ols="judgeXyear "
global cluster_iv="judgeXyear "
global instrument="stringency_n100_loo"
global fe="districtXquarter"

macro define controls="noprior joint ad_damnum ageatcase age_sq age_cub fem1 fem1_miss neigh_pov_rate_5yr nbrhd_med_rent_5yr neigh_pov_rate_5yr_miss nbrhd_med_rent_5yr_miss"
local outcomes="fin_health VANTAGE_V3_SCORE zerorevolving ALL5042 ANY_AUT_OR_LEASE "

use unique_id evicted  $cluster_ols  $cluster_iv $fe $instrument $controls ageatcase age_sq age_cub ageatcase_miss *full_samp* VANTAGE_V3_SCORE* ALL5042_* zerorevolving* COL5060* ANY_AUT_OR_LEASE* fin_health_* inquiries* any_inquiries* accounts* any_accounts* stringency_amount judgment_amount exp_indiv_id filing_year using "${build}/iv_fin_strain.dta", clear

keep if ageatcase>=18 & ageatcase<=55
rename fem1 fem

gen great_recession=(filing_year>=2008 & filing_year<=2012)

***********************
* MAIN IV REGRESSIONS *
***********************
gen all=1

foreach pop in  "great_recession" {
	foreach popval of numlist 0 1  {
			qui foreach var in `outcomes' {
				foreach rng in "1_4" "5_8"  {

				*Label variables
				if "`var'"=="fin_health" label var `var'_`rng' "Financial health index"
				if "`var'"=="VANTAGE_V3_SCORE" label var `var'_`rng' "Credit Score"
				if "`var'"=="zerorevolving" label var `var'_`rng' "No Open Revolving Account"
				if "`var'"=="ALL5042" label var `var'_`rng' "Total bal. delinquencies or collections"
				if "`var'"=="ANY_AUT_OR_LEASE" label var `var'_`rng' "Any Auto Loan or Lease"
				local vname=`"`:variable label `var'_`rng''"'	

				local lags="`var'_b1_8 `var'_b1_8_miss `var'_b1 `var'_b2 `var'_b3 `var'_b4 `var'_b1_miss `var'_b2_miss `var'_b3_miss `var'_b4_miss "
				recode `var'_b1_8 (.=-1)
				
				**All specifications are restricted to the same obs as the main IV specification*
				ivregress 2sls `var'_`rng' i.$fe $controls (evicted=$instrument) if `pop'==`popval', r cluster($cluster_iv) 
				cap drop samplerestriction
				gen samplerestriction=e(sample)
				
				*NON-EVICTED MEAN
				sum `var'_`rng' if samplerestriction==1 & evicted==0 & `pop'==`popval'
					local y_base_mean=r(mean)
					local y_base_sd=r(sd)
				unique exp_indiv_id if samplerestriction==1 & evicted==0 & `pop'==`popval'
					local y_base_n=r(sum)
				sum `var'_`rng' if samplerestriction==1 & `pop'==`popval'
					if "`pop'"=="all" local sd_y_all=r(sd)
					if "`pop'"~="all" local sd_y_all=.
				sum evicted if samplerestriction==1 & `pop'==`popval'
					if "`pop'"=="all" local sd_evict_all=r(sd)
					if "`pop'"~="all" local sd_evict_all=.
				local label="Pop: `pop'=`popval', Outcome: `vname', Outcome window: `rng'"	
				frame post results ("`label'")(.) (.) (.) (`y_base_mean') (`y_base_sd') (`y_base_n') (`sd_y_all')(`sd_evict_all')(.)(.)
				
				**OLS
				reg  `var'_`rng' evicted $controls `lags' i.$fe  if `pop'==`popval' & samplerestriction==1, r cluster($cluster_ols)
				local b_evict=_b[evicted] 
				local se_evict=_se[evicted] 
				local n_obs=e(N)
				local label="Pop: `pop'=`popval', Outcome: `vname', Outcome window: `rng', Spec: OLS baseline w/ additional controls" 
				frame post results ("`label'") (`b_evict') (`se_evict') (`n_obs') (.) (.) (.) (.) (.) (.)(.)
			
				**IV
				ivregress 2sls `var'_`rng' $controls `lags' i.$fe  (evicted=$instrument) if `pop'==`popval' & samplerestriction==1, r cluster($cluster_iv) 
				local b_evict=_b[evicted] 
				local se_evict=_se[evicted] 
				local n_obs=e(N)
				local label="Pop: `pop'=`popval', Outcome: `vname', Outcome window: `rng', Spec: IV baseline w/ additional controls" 
				frame post results ("`label'") (`b_evict') (`se_evict') (`n_obs') (.) (.) (.) (.) (.) (.)(.)	
						
				}
			}
	}
}

frame results: export delimited using "${Inputs}\Cook_credit_ivols_gr.csv", replace
